package com.imooc.demo.web;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.imooc.demo.entity.Category1;
import com.imooc.demo.service.Category1Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//RestController是Controller和ResponseBody的组合
@RestController
@RequestMapping("/category1")//指定路由
public class Category1Controller {
    @Autowired
    private Category1Service category1Service;
    //获取所有种类的信息，value = "listcategory1"是访问url路由
    @RequestMapping(value = "listcategory1",method = RequestMethod.GET)
    private  Map<String , Object>listCategory1(){
        Map<String , Object>modelMap = new HashMap<String , Object>();
        List<Category1> list = category1Service.getCategory1List();
        modelMap.put("category1List" , list);
        return  modelMap;
    }

    //通过种类信息获取种类信息
    @RequestMapping(value = "getcategory1byid",method = RequestMethod.GET)
    private  Map<String,Object>  getCategory1ById(Integer category1Id){
        Map<String,Object>  modelMap = new HashMap<String ,Object>();
        Category1 category1= category1Service.getCategory1ById(category1Id);
        modelMap.put("category1",category1);
        return modelMap;
    }

    //添加种类信息
    @RequestMapping(value = "/addcategory1",method = RequestMethod.POST)
    //@RequestBody标识接收的内容除了xwww from url encoding 编码内容之外的一些内容，如json，xml等,此处需传入xml
    private  Map<String,Object>  addCategory1(@RequestBody Category1 category1){
        Map<String,Object>  modelMap = new HashMap<String ,Object>();
        modelMap.put("success",category1Service.addCategory1(category1));
        return modelMap;
    }

    //修改种类信息
    @RequestMapping(value = "/modifycategory1",method = RequestMethod.POST)
    //@RequestBody标识接收的内容除了xwww from url encoding 编码内容之外的一些内容，如json，xml等
    private  Map<String,Object>  modifyCategory1(@RequestBody Category1 category1)
            throws JsonParseException , JsonMappingException , IOException {
        Map<String,Object>  modelMap = new HashMap<String ,Object>();
        //修改种类信息 ，"success"保存操作状态，如category1Service.modifyCategory1(category1)操作成功返回true，否则false
        modelMap.put("success",category1Service.modifyCategory1(category1));
        return modelMap;
    }

    //删除种类信息
    @RequestMapping(value = "/removecategory1",method = RequestMethod.POST)
    //@RequestBody标识接收的内容除了xwww from url encoding 编码内容之外的一些内容，如json，xml等,此处需传入xml
    private  Map<String,Object>  removeCategory1(Integer category1Id){
        Map<String,Object>  modelMap = new HashMap<String ,Object>();
        //修改种类信息 ，"success"保存操作状态，如category1Service.deleteCategory1(category1Id)操作成功返回true，否则false
        modelMap.put("success",category1Service.deleteCategory1(category1Id));
        return modelMap;
    }

    // 一对多查询
    @RequestMapping(value = "selectcategory1byid",method = RequestMethod.GET)
    private  Map<String,Object>  selectCategory1ById(Integer category1Id){
        Map<String,Object>  modelMap = new HashMap<String ,Object>();
        Category1 category1= category1Service.selectCategory1ById(category1Id);
        modelMap.put("category1",category1);
        return modelMap;
    }










}
